#ifndef MYSCENE_H
#define MYSCENE_H

#include <QUdpSocket>

#include <QGraphicsScene>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include "gameprocess.h"
#include <QGraphicsTextItem>
#include <QGraphicsPixmapItem>
#include "userinfodialog.h"

class MyScene: public QGraphicsScene
{
public:
    MyScene(UserInfoDialog* dialog);
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
private:
    UserInfoDialog* dialog;

    QString localIp;
    bool isActive;
    QUdpSocket* udpSocket;
    quint16 port;
    QHostAddress groupAddress;
    // QVector<QString> ipList; //进入准备状态的ip集合，可能包含自己

    bool isPressed = false; // 按键正处于按住的状态
    bool isSelectingCard = false; //是否正在范围选中牌

    // 按下滑动起始和终止坐标
    int startIndex;
    int endIndex;
    int lastIndex; // 移动过程中掠过的上一张牌

    int order; // 代表进入房间的顺序

    GameProcess gameProcess;

    // 开始游戏按钮
    QGraphicsPixmapItem* startBtn;

    // 出牌按钮
    QGraphicsPixmapItem* outCardBtn;

    // 不出按钮
    QGraphicsPixmapItem* passBtn;

    // 举手 0自己 1右上 2左上
    QGraphicsPixmapItem* hand[3];

    QGraphicsTextItem* lastItem1;

    QGraphicsTextItem* lastItem2;

    QGraphicsTextItem* waitInfo; //等待其他玩家加入提示

    // 取消选牌
    void cancelSelectCard();

    void udpSend(const QJsonObject &sendObj);

    void udpSingleSend(const QJsonObject &sendObj, QString ip);

    void init(); //游戏初始化(用于重新开始)

    // 绘制头像和昵称
    QGraphicsPixmapItem* drawAvatarAndName(int avatarNum, QPointF pos, QString name);
private slots:
    void onUdpReadyRead();
};

#endif // MYSCENE_H
